//
//  TodayInspectionVC.swift
//  Employee
//
//  Created by swordjoy on 2017/3/23.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import SwiftyJSON
import Moya

class TodayInspectionVC: BaseVC {

  var parentVC: UIViewController!
  
  fileprivate var inspections: [Inspection] = []
  fileprivate var tableView: UITableView?
    
  fileprivate var cancle: Cancellable?
  
  override func viewDidLoad() {
      super.viewDidLoad()
    view.backgroundColor = GlobalConfig.BGWhiteColor
    
    create()
  }
  
  override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    cancle?.cancel()
  }
  
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    
  deinit {
    cancle?.cancel()
    GlobalConfig.notificationCenter.removeObserver(self)
  }

}

// MARK: - action

extension TodayInspectionVC {
    @objc func updateData(by isHeader: Bool = false) {
        cancle = WorkorderRequest.shared.getInspectionPlans(date: Date().inLocalRegion().string(custom: "yyyy-MM-dd"), success: { (inspections) in
            self.inspections = inspections
                runOnMainThread {
                    if isHeader {
                        self.tableView?.mj_header.endRefreshing()
                    }
                    self.tableView?.reloadData()
                }

            }) {
                if isHeader {
                    runOnMainThread {
                        self.tableView?.mj_header.endRefreshing()
                    }
                }
            }
    }
  
}

// MARK: - fileprivate

extension TodayInspectionVC {
  fileprivate func create() {
    
    tableView = UITableView()
    tableView?.backgroundColor = UIColor.clear
    tableView?.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: UIScreen.noBarHeight - 54)
    tableView?.delegate = self
    tableView?.dataSource = self
    tableView?.separatorStyle = .none
    tableView?.register(InspectionCell.self, forCellReuseIdentifier: InspectionCell.identifier)
    
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 4))
    headerView.backgroundColor = GlobalConfig.BGWhiteColor
    tableView?.tableHeaderView = headerView
    
    
    tableView?.mj_header = CustomRefreshHeader(refreshingBlock: { [weak self] in self?.updateData(by: true) })
    
    view.addSubview(tableView!)
    
    GlobalConfig.notificationCenter.addObserver(self, selector: #selector(self.updateData(by:)), name: Notification.Name.updateHome.updateHomeInspectionData, object: nil)
  }
}

// MARK: - UITableViewDataSource

extension TodayInspectionVC : UITableViewDataSource {
  func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return inspections.count
  }
  
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: InspectionCell.identifier) as! InspectionCell
    cell.model = inspections[indexPath.row]
    return cell
  }
}

// MARK: - UITableViewDelegate

extension TodayInspectionVC : UITableViewDelegate {
  func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return InspectionCell.height(with: inspections[indexPath.row])
  }
  
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let data = inspections[indexPath.row]
    
    let vc = InspectionDetailVC()
    vc.status = data.status
    vc.isCooperate = data.cooperate
    vc.selectDate = Date()
    vc.isFromHome = true
    vc.parentVC = self.parentVC
    vc.inspectionTime = data.timeInterval
    if vc.status == "WaitInspection" {
        vc.recordId = inspections[indexPath.row].planId }
    else {
        vc.recordId = inspections[indexPath.row].recordId }
    
    parentVC.navigationController?.pushViewController(vc, animated: true)
  }
  
  func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    
    let fixView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.width, height: 1))
    fixView.backgroundColor = GlobalConfig.BGWhiteColor
    return fixView
    
  }
  
  func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 1
  }
  
}

